/*---------Bread Crumb 3.0------ */
/*
Author: pbijesh@cordys.com
Date: 04-04-2012
NOTE: This feature is implemnted by introducing the level attribute in the application defenition
* 'onapplicationstatechange' event(Platform generated event) has been used and with loaded and unloaded states to add and remove the crumbs.
* it can be included in any DIV in an xform by creating a breadCrumb object and initialzing it with the DIV id.ie, the breadcrumb container.
*Ex: 	var newCrumb = new BreadCrumb("holderId",appSelCallback);
		newCrumb.initBreadCrumb();
*/
function BreadCrumb(containerId, appSelectCalback)
{
	this.container = document.getElementById(containerId);
	this.currentLevel = -1;
	if (appSelectCalback) this.container.appselCalbackHook = appSelectCalback;
}
BreadCrumb.prototype.initBreadCrumb = function ()
{
	/*Add the Listner for on onapplicationstatechange  */
	system.addListener("onapplicationstatechange", handleApplicationStateHandler(this));
}
BreadCrumb.prototype.addBreadCrumb = function (appdefinition, appLevel)
{
	if (appLevel <= this.currentLevel) this.resetBreadCrumb(appLevel);
	var crumbHolder = document.createElement("span");
	crumbHolder.className = "crumb-holder";
	crumbHolder.id = "" + appLevel;
	crumbHolder.applicationDefition = appdefinition;
	applicationCaption = cordys.getNodeText(appdefinition, ".//*[local-name()='name']");
	var crumbStruct = "<a href='#' onclick='openApplicationFrmCrumb(event)'>" + applicationCaption + "</a>"
	var extender = "<label class='crumb-separator'>&gt;</label>";
	crumbHolder.innerHTML = (appLevel == 0) ? crumbStruct : extender + crumbStruct;
	this.container.appendChild(crumbHolder);
	this.currentLevel = appLevel;
}
BreadCrumb.prototype.resetBreadCrumb = function (level)
{
	var crumb, crumbId;
	for (var i = this.currentLevel; i >= level; i--)
	{
		crumbId = i.toString();
		crumb = document.getElementById(crumbId);
		if (crumb) crumb.parentNode.removeChild(crumb);
	}
	this.currentLevel = level - 1;
}
BreadCrumb.prototype.stepBack = function ()
{
	if (this.currentLevel == 0) return;
	var currentCrumbId = (this.currentLevel).toString();
	var targetCrumb = document.getElementById(currentCrumbId).previousSibling;
	application.select(targetCrumb.applicationDefition);
}

function handleApplicationStateHandler(breadCrumbObj)
{
	return function (appObj)
	{
		if (appObj.applicationState == "loaded")
		{
			var appdefinition = system.containers[appObj.applicationId].guiRepresentation.applicationDefinition;
			var appLevel = cordys.getNodeText(appdefinition, ".//*[local-name()='level']");
			if (appdefinition && appLevel)
			{
				breadCrumbObj.addBreadCrumb(appdefinition, appLevel);
			}
		}
	}
}

function openApplicationFrmCrumb(eventObject)
{
	var eventObject = eventObject ? eventObject : window.event;
	var crumbItem = cordys.getEventTarget(eventObject).parentNode;
	var appselCalbackHook = crumbItem.parentNode.appselCalbackHook;
	if (!appselCalbackHook)
	{
		application.select(crumbItem.applicationDefition);
	}
	else
	{
		application.select(crumbItem.applicationDefition, appselCalbackHook(crumbItem.applicationDefition));
	}
}